using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ApplicationLibrary.ExtendedControls
{
    /// <summary>
    /// Summary description for LookUpComboBox.
    /// </summary>
    [ToolboxBitmap(typeof(System.Windows.Forms.TextBox))]
    [Designer(typeof(MySnapLinesDesigner))]
    public class LookUpComboBox : System.Windows.Forms.UserControl, ISnapLineControl
    {
        public Control textBoxValue { get { return textBox1; } }

        public Control labelValue { get { return null; } }

        protected StringTextBox textBox1;
        private System.Windows.Forms.Button button1;

        //public delegate void TextChanged(object sender, EventArgs e);
        //public event TextChanged OnTextChanged;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        private Control descriptionControl = null;

        protected SortedList<string, string> SL = null;
        private string lookupCode = string.Empty;

        #region EventHandler

        new public event EventHandler GotFocus;

        new public event EventHandler LostFocus;

        public event EventHandler PageUp;

        public event EventHandler PageDown;

        #endregion

        #region Public Properties

        public Control DescriptionControl { set { descriptionControl = value; } }

        public override string Text
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public TextBox rdrTxtBox { get { return textBox1; } }

        public int MaxLength { set { textBox1.MaxLength = value; } }

        //public bool		IsInDataGrid
        //{
        //    set { textBox1.IsInDataGrid = value;}
        //    get { return textBox1.IsInDataGrid; }
        //}

        public BorderStyle rdrBorderStyle
        {
            set { textBox1.BorderStyle = value; }
            get { return textBox1.BorderStyle; }
        }

        public void Clear()
        {
            SL.Clear();
        }

        public void AddElement(string code, string description)
        {
            SL.Add(code, description);
        }

        public void AddElements(Dictionary<string, string> sl)
        {
            if (SL.Count == 0)
                foreach (KeyValuePair<string, string> kvp in sl)
                    SL.Add(kvp.Key, kvp.Value);
        }

        public int SelectionStart
        {
            get { return textBox1.SelectionStart; }
            set { textBox1.SelectionStart = value; }
        }

        public int SelectionLength
        {
            get { return textBox1.SelectionLength; }
            set { textBox1.SelectionLength = value; }
        }

        public bool ReadOnly
        {
            get { return textBox1.ReadOnly; }
            set
            {
                textBox1.ReadOnly = value;
                button1.Enabled = !value;
            }
        }

        #endregion

        public LookUpComboBox()
            : base()
        {
            SL = new SortedList<string, string>();

            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new StringTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();

            //
            // textBox1
            //
            this.textBox1.AcceptsReturn = true;
            this.textBox1.AcceptsTab = true;
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(72, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "";
            this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
            this.textBox1.LostFocus += new System.EventHandler(this.textBox1_LostFocus);
            this.textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);

            //this.textBox1.PageDown+=new EventHandler(textBox1_PageDown);
            //this.textBox1.PageUp+=new EventHandler(textBox1_PageUp);
            this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
            this.textBox1.Validated += new System.EventHandler(this.textBox1_Validated);
            this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

            //
            // button1
            //
            this.button1.Dock = System.Windows.Forms.DockStyle.Right;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.button1.Font = new System.Drawing.Font("Webdings", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(2)));
            this.button1.Location = new System.Drawing.Point(74, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(22, 22);
            this.button1.TabIndex = 1;
            this.button1.TabStop = false;
            this.button1.Text = "6";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            //this.button1.LostFocus += new System.EventHandler(this.button1_LostFocus);
            //this.button1.GotFocus += new System.EventHandler(this.button1_GotFocus);
            //
            // LookUpComboBox
            //
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "LookUpComboBox";
            this.Size = new System.Drawing.Size(96, 22);
            this.Resize += new System.EventHandler(this.LookUpComboBox_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.LookUpComboBox_Paint);
            this.ResumeLayout(false);
            this.GotFocus += new EventHandler(LookUpComboBox_GotFocus);
        }

        #endregion

        private void LookUpComboBox_Resize(object sender, System.EventArgs e)
        {
            button1.Width = textBox1.Height;
            button1.Height = textBox1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;
            button1.Font = new Font("Webdings", this.Font.GetHeight() - this.Font.GetHeight() / 4);
            this.Height = textBox1.Height;
        }

        private void LookUpComboBox_Paint(object sender, PaintEventArgs e)
        {
            button1.Width = textBox1.Height;
            button1.Height = textBox1.Height;
            button1.Left = this.Size.Width - button1.Width;
            button1.Top = textBox1.Top;
            textBox1.Width = this.Width - button1.Width;
            button1.Font = new Font("Webdings", this.Font.GetHeight() - this.Font.GetHeight() / 4);
            this.Height = textBox1.Height;
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F10)
                ApriCombo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ApriCombo();
        }

        private void ApriCombo()
        {
            PopUpComboBox popup = new PopUpComboBox(SL, textBox1.Text);
            popup.AfterRowSelectEvent += new PopUpComboBox.AfterRowSelectEventHandler(popup_AfterRowSelectEvent);
            popup.Closed += new EventHandler(popup_Closed);
            popup.Location = location();
            popup.Show();
        }

        private Point location()
        {
            int X = this.PointToScreen(textBox1.Location).X;
            int Y = this.PointToScreen(textBox1.Location).Y + textBox1.Size.Height;

            return new Point(X, Y);
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (textBox1.Text != "" && !SL.ContainsKey(textBox1.Text))
            {
                MessageBox.Show("Code not found");
                e.Cancel = true;
            }
        }

        private void textBox1_Validated(object sender, EventArgs e)
        {
            if (descriptionControl != null && textBox1.Text != "")
                descriptionControl.Text = SL[textBox1.Text];
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
        }

        private void popup_AfterRowSelectEvent(object sender, string SelectedValue)
        {
            textBox1.Focus();
            textBox1.Text = SelectedValue;
        }

        private void popup_Closed(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        #region Raise Event

        private void button1_GotFocus(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_GotFocus(object sender, EventArgs e)
        {
            if (this.GotFocus != null)
                this.GotFocus(this, e);
        }

        private void textBox1_LostFocus(object sender, EventArgs e)
        {
            if (this.LostFocus != null)
                this.LostFocus(this, e);
        }

        private void button1_LostFocus(object sender, EventArgs e)
        {
            if (this.LostFocus != null)
                this.LostFocus(this, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (descriptionControl != null)
            {
                if (textBox1.Text != "" && SL.ContainsKey(textBox1.Text))
                    descriptionControl.Text = SL[textBox1.Text].ToString();
                else
                    descriptionControl.Text = "";
            }

            this.OnTextChanged(e);

            //this.TextChanged(this, e);
        }

        private void textBox1_PageDown(object sender, EventArgs e)
        {
            if (this.PageDown != null)
                this.PageDown(this, e);
        }

        private void textBox1_PageUp(object sender, EventArgs e)
        {
            if (this.PageUp != null)
                this.PageUp(this, e);
        }

        #endregion

        private void LookUpComboBox_GotFocus(object sender, EventArgs e)
        {
            this.textBox1.Focus();
        }
    }
}